package webgamemaker.FightCanvas;

public class FighterActionJump implements FighterAction 
{
	public int x1, y1, distX;
	public float speed, gravity;
	public FighterActionJump(int x1, int y1, int distX, float speed, float gravity)
	{
		this.x1 = x1;
		this.y1 = y1;
		this.distX = distX;
		this.speed = speed;
		this.gravity = gravity;
		//x = x1+t*speed
		//y(0) = y1;
		//y(t2) = y1;
		//y(t2/2) = y1+height;
		//y'(t2/2) = 0
		//y'' = -gravity
		//y'(t) = ((t2/2)-t)*gravity
		//y(t) = y1+((t2/2)*t-t^2/2)*gravity
		//t = (x-x1)*(x2-x1)/speed 
		
	}
	public void apply(Fighter fighter) 
	{
		int x2 = x1+distX;
		if(fighter.x > x2)
			fighter.x -= Math.min(speed, fighter.x-x2);
		else if(fighter.x < x2)
			fighter.x += Math.min(speed, x2-fighter.x);
		float t = (fighter.x - x1)/speed;
		fighter.y = (int) (y1-(distX*t*gravity/(2*speed) - t*t*gravity/2));
	}
	
	public boolean isFinished(Fighter fighter)
	{
		return fighter.x == x1+distX;
	}

}
